package com.wfm.lock;

import java.util.concurrent.locks.StampedLock;

public class StampedLockStateExample {
    private static final StampedLock stampedLock = new StampedLock();

    public static void main(String[] args) {
        // 初始状态

        // 乐观读
        long optimisticStamp = stampedLock.tryOptimisticRead();
        System.out.println("乐观读戳记: " + Long.toBinaryString(optimisticStamp));

        // 获取读锁
        long readStamp = stampedLock.readLock();
        System.out.println("读锁戳记: " + Long.toBinaryString(readStamp));

        // 释放读锁
        stampedLock.unlockRead(readStamp);

        // 获取写锁
        long writeStamp = stampedLock.writeLock();
        System.out.println("写锁戳记: " + Long.toBinaryString(writeStamp));

        // 释放写锁
        stampedLock.unlockWrite(writeStamp);
    }
}